package com.ddyx.common;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendBatchSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.ddyx.SMS.entity.SMS;
import com.ddyx.SMS.mapper.SMSMapper;
import com.ddyx.SMS.service.IALISMSService;
import com.ddyx.SMS.service.impl.ALISMSServiceService;
import com.ddyx.monitor.entity.DomainServer;
import com.ddyx.monitor.entity.DomainServerLogs;
import com.ddyx.monitor.entity.DomainServerLogsVo;
import com.ddyx.monitor.entity.DomainServerMobile;
import com.ddyx.monitor.entity.ResultMap;
import com.ddyx.monitor.mapper.DomainServerLogsMapper;
import com.ddyx.monitor.mapper.DomainServerMapper;
import com.ddyx.monitor.mapper.DomainServerMobileMapper;



/**
 * @ClassName:  TimeHandlerUtil   
 * @Description:  定时任务  
 * @author: JiangShuai 
 * @date:   2018年1月30日 下午5:29:05     
 * @Copyright: 2018 dingdian.parllay.cn Inc. All rights reserved. 
 * 注意：本内容仅限于叮当一下(北京)信息技术有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Component
public class TimeHandlerUtil {

	@Autowired
	private DomainServerMapper domainServerMapper;
	
	@Autowired
	private DomainServerLogsMapper domainServerLogsMapper;
	
	@Autowired
	private DomainServerMobileMapper domainServerMobileMapper;
	
	@Autowired
	private IALISMSService smsService;
	
	@Autowired
	private SMSMapper smsMapper;
	
	/**
	 * @Title: editArrivalCount   
	 * @Description: 半小时执行一次 
	 * @author: JiangShuai
	 * @param: @throws Exception
	 * @return: void
	 */
	//@Lazy(true)
	//@Scheduled(cron="0 */30 * * * ?")
	@Scheduled(cron="0 0/2 * * * ?")
	public void loopHttp() throws Exception {
		DomainServer bean=new DomainServer();
		bean.setStatus(1);
		List<DomainServer> DomainServerList=domainServerMapper.selectDomainServerList(bean);
		if(null!=DomainServerList && !DomainServerList.isEmpty()) {
			List<DomainServerLogs> DomainServerLogsList=new ArrayList<>();
			for (int i = 0; i < DomainServerList.size(); i++) {
				DomainServer ds=DomainServerList.get(i);
				ResultMap resultMap=new HttpHandle().doPostHandle(ds);
				DomainServerLogs dsl=new DomainServerLogs();
				dsl.setCode(resultMap.getCode());
				dsl.setMsg(resultMap.getMsg());
				dsl.setCreateTime(new java.util.Date());
				dsl.setServerId(ds.getServerId());
				dsl.setServerName(ds.getServerName());
				dsl.setServerEffect(ds.getEffect());
				dsl.setRequestTime(resultMap.getRequestTime());
				domainServerLogsMapper.insertDomainServerLogs(dsl);
				if(1000!=resultMap.getCode()) {
					DomainServerLogsList.add(dsl);
				}
			}
			//出现异常
			if(null!=DomainServerLogsList && !DomainServerLogsList.isEmpty()) {
				List<DomainServerLogsVo> DomainServerLogsVoList=new ArrayList<>();
				for (int i = 0; i < DomainServerLogsList.size(); i++) {
					DomainServerLogs lo=DomainServerLogsList.get(i);
					bean.setServerId(lo.getServerId());
					DomainServer dsr=domainServerMapper.selectDomainServerInfo(bean);
					//写入日志表
					DomainServerLogsVo vo=new DomainServerLogsVo();
					vo.setCode(lo.getCode());
					vo.setCreateTime(lo.getCreateTime());
					vo.setDesc(dsr.getDesc());
					vo.setEffect(dsr.getEffect());
					vo.setFactIp(dsr.getFactIp());
					vo.setMethodName(dsr.getMethodName());
					vo.setMsg(lo.getMsg());
					vo.setPort(dsr.getPort());
					vo.setProxyIp(dsr.getProxyIp());
					vo.setRequestTime(lo.getRequestTime());
					vo.setServerId(lo.getServerId());
					vo.setServerLogId(lo.getServerLogId());
					vo.setServerName(dsr.getServerName());
					vo.setStatus(dsr.getStatus());
					DomainServerLogsVoList.add(vo);
				}

				//发预警短信
				/*String content="";
				for (int i = 0; i < DomainServerLogsVoList.size(); i++) {
					DomainServerLogsVo vo=DomainServerLogsVoList.get(i);
					content+=(vo.getDesc()+","+vo.getFactIp()+";");
				}
				DomainServerMobile dobean=new DomainServerMobile();
				dobean.setStatus(1);
				List<DomainServerMobile> mobileList=domainServerMobileMapper.selectDomainServerMobileList(dobean);
				if(null!=mobileList && !mobileList.isEmpty()) {
					for (int i = 0; i < mobileList.size(); i++) {
						DomainServerMobile domobile=mobileList.get(i);
						String mobile=domobile.getMobile();
						if(StringUtils.isNotBlank(mobile) && !"null".equals(mobile)) {
							content=content.substring(0, content.length()-1);
							JSONObject json=new JSONObject();
							json.put("code", content.substring(0, content.length()-1));
							SendSmsRequest request = new SendSmsRequest();
							request.setPhoneNumbers(mobile);
							request.setSignName("叮当一下");
							request.setTemplateCode("SMS_130750101");
							request.setTemplateParam("{\"code\":\""+content+"\"}");
							Boolean bo=smsService.sendSMS(request);
							
							//保存短信日志
							SMS sm=new SMS();
							sm.setCreateTime(new Date());
							sm.setScene("实时监测项目运行");
							sm.setContent(content);
							sm.setFromName("系统");
							if(bo){
								sm.setResult(1);
							}else{
								sm.setResult(0);
							}
							sm.setToNumber(mobile);
							smsMapper.insertSMS(sm);
						}
					}
				}*/
				
				//获取邮件发送对象的email拼接字符串
				String emailStr=domainServerMobileMapper.selectEmailStr();
				//发送预警邮件(html类型不带附件)
				new HttpHandle().doEmailHtmlHandle(DomainServerLogsVoList, emailStr);
			}
		}
	}

	//@Scheduled(cron="0 0 2 * * ?")
	@Scheduled(cron="0 */2 * * * ?")
	public void test() throws Exception {
		String tim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()); 
		System.out.println(tim);

		/*SendSmsRequest request = new SendSmsRequest();
		request.setPhoneNumbers("18618488086,17538510770");
		request.setSignName("叮当一下");
		request.setTemplateCode("SMS_130750101");
		request.setTemplateParam("{\"code\":\"123cesshi\"}");
		smsService.sendSMS(request);*/
	}

}
